<?php
$fm_id = $route->bodyParams['fm_id'];
$fm_install_dir = $route->bodyParams['fm_install_dir'];

//更新菜单的安装状态
$updateParams = array(
    "update_where" => "fm_id",
    "fm_id"        => $fm_id,
    "fm_is_menu"   => 1
);

$UPDATE_RS = $db->mysqlDB->automaticUpdate($updateParams)->update("sys_organizational_function_module");

if($UPDATE_RS){
    $response->responseData(false);
}

$menuCreateResult = true;

$addMenuDataPath = $fm_install_dir."/menuData/menu.json";
if(!check_file_exists($addMenuDataPath)){
    $response->responseData(false,"没有找到菜单相关信息：".$addMenuDataPath);
}

$addMenuDataText = file_get_contents($addMenuDataPath);
$addMenuData     = json_decode($addMenuDataText,true);

for($i=0;$i<count($addMenuData);$i++){

    $level_1_menuData = $addMenuData[$i];

    $addParams = array(
        "m_name" => $level_1_menuData['m_name'],
        "m_url"  => $level_1_menuData['m_url'],
        "m_icon" => $level_1_menuData['m_icon'],
        "m_path" => "0,",
        "m_type" => "1",
        "m_fmid" => $fm_id
    );  

    //新增一级菜单操作
    $R1 = $db->mysqlDB->insert("sys_menu",$addParams);

    if(!$R1){
        $menuCreateResult = false;
    }

    $level_2_menuData = $level_1_menuData['child'];

    //新增二级菜单
    for($j=0;$j<count($level_2_menuData);$j++){

        $level2_menuItem = $level_2_menuData[$j];

        $addParams = array(
            "m_name"  => $level2_menuItem['m_name'],
            "m_url"   => $level2_menuItem['m_url'],
            "m_icon"  => $level2_menuItem['m_icon'],
            "m_level" => 1,
            "m_fid"   => $R1,
            "m_path"  => "0,".$R1.",",
            "m_type"  => "1",
            "m_fmid" => $fm_id
        );  

        //新增二级菜单操作
        $R2 = $db->mysqlDB->insert("sys_menu",$addParams);

        if(!$R2){
            $menuCreateResult = false;
        }

        //新增三级菜单
        $level3_menuData = $level2_menuItem['child'];

        for($k=0;$k<count($level3_menuData);$k++){

            $level3_menuItem = $level3_menuData[$k];

            $addParams = array(
                "m_name"      => $level3_menuItem['m_name'],
                "m_url"       => $level3_menuItem['m_url'],
                "m_icon"      => $level3_menuItem['m_icon'],
                "m_route_url" => $level3_menuItem['m_route_url'],
                "m_level"     => 2,
                "m_fid"       => $R2,
                "m_path"      => "0,".$R1.",".$R2.",",
                "m_type"      => "1",
                "m_fmid"      => $fm_id
            );

            //新增三级菜单操作
            $R3 = $db->mysqlDB->insert("sys_menu",$addParams);

            if(!$R3){
                $menuCreateResult = false;
            }

        }
    }
}

if(!$menuCreateResult){
    $response->responseData(false);
}else{
    $response->responseData(true);
}
